﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using Ini;


namespace Sequence
{
    public partial class FrmOpenProcess : Form
    {
        public string sId ;
        public DbConnection m_connection;
        public IniFile oIniFile = null;
        private string sIniFilePath = "C:\\Sapiens\\Sequence\\Sequence.ini";

        private DbConnection GetConntion()
        {
            return m_connection;
        }

        public FrmOpenProcess()
        {
        }

        public void Init()
        {
            oIniFile = new IniFile(sIniFilePath);
            TxtFilter.Text = oIniFile.IniReadValue("OpenProcessFilter", "Text", "_");
        }

        public FrmOpenProcess(DbConnection oConnection)
        {
            InitializeComponent();
            m_connection = oConnection;
        }

        public DbConnection GetConnection()
        {
            return m_connection;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        private void FrmOpenProcess_Load(object sender, EventArgs e)
        {
        }

        private void LoadProcess()
        {
            String sSql = ""; 
            
            try
            {
                DbCommand command = GetConntion().CreateCommand();
                sSql = CSqlString.GetProcess(CSqlString.GetConnectionType(GetConntion().ConnectionString),TxtFilter.Text);
                command.CommandText = sSql;
                command.CommandType = CommandType.Text;
                DbDataReader oDataReader = command.ExecuteReader();

                listView1.Items.Clear();txtSelected.Text = ""; sId = "";
                while (oDataReader.Read())
                {
                    ListViewItem oListViewItem = listView1.Items.Add(oDataReader["ID"].ToString());
                    oListViewItem.SubItems[0].Text = oDataReader["ID"].ToString();
                    oListViewItem.SubItems.Add(oDataReader["NAME"].ToString());

                }
                oDataReader.Close();
                oDataReader.Dispose();
                command.Dispose();

            }
            catch (Exception ex)
            {
                DialogResult dialogResult = MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            oIniFile.IniWriteValue("OpenProcessFilter", "Text" ,TxtFilter.Text);
            this.Close();
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            oIniFile.IniWriteValue("OpenProcessFilter", "Text" ,TxtFilter.Text);
            this.Close();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                string sError = CSqlString.RunSql("delete from t_api_qd_process where id=" +listView1.SelectedItems[0].Text, GetConnection());
                sError = CSqlString.RunSql("delete from t_api_qd_process_step where process_id=" + listView1.SelectedItems[0].Text, GetConnection());
                sError = CSqlString.RunSql("delete from t_api_qd_process_step_params where process_id=" + listView1.SelectedItems[0].Text, GetConnection());
                LoadProcess();
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            oIniFile.IniWriteValue("OpenProcessFilter", "Text", TxtFilter.Text);
            this.Close();
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {

        }

        private void toolStripTextBox1_Click(object sender, EventArgs e)
        {
            
        }

        private void toolStripTextBox1_TextChanged(object sender, EventArgs e)
        {
            LoadProcess();
        }

        private void listView1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            try
            {
                if (listView1.SelectedItems.Count > 0)
                {

                    sId = listView1.SelectedItems[0].Text;
                    string sName = listView1.SelectedItems[0].SubItems[1].Text;
                    txtSelected.Text = sId + "\t" + sName;

                }
            }
            catch (Exception ex)
            {
                DialogResult dialogResult = MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            oIniFile.IniWriteValue("OpenProcessFilter", "Text", TxtFilter.Text);
            this.Close();
        }

        private void cloanToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                string sSql = @"
Begin
INSERT INTO T_API_QD_PROCESS 
(SELECT (SELECT max(id) + 1 FROM T_API_QD_PROCESS), EFFEC_DATE, TO_EFFEC_DATE, LAST_UPDATE_USER_ID, LAST_UPDATE_REG_DATE, NAME || '-Clone'
FROM T_API_QD_PROCESS
WHERE ID=$ProcessId$);
INSERT INTO T_API_QD_PROCESS_STEP
(SELECT (SELECT max(id)  FROM T_API_QD_PROCESS), ID, EFFEC_DATE, TO_EFFEC_DATE, LAST_UPDATE_USER_ID, LAST_UPDATE_REG_DATE, NAME, ""TYPE"", QUERY_NAME, STEP_MODE
FROM T_API_QD_PROCESS_STEP WHERE PROCESS_ID=$ProcessId$);
INSERT INTO T_API_QD_PROCESS_STEP_PARAMS
(SELECT (SELECT max(id)  FROM T_API_QD_PROCESS), STEP_ID, ID, EFFEC_DATE, TO_EFFEC_DATE, LAST_UPDATED_USER_ID, LAST_UPDATED_REG_DATE, NAME, PARAMS_TYPE, PARAMS_VALUE, DEFAULT_VALUE
FROM T_API_QD_PROCESS_STEP_PARAMS WHERE PROCESS_ID=$ProcessId$);
End;
";
                sSql = sSql.Replace("$ProcessId$", listView1.SelectedItems[0].Text);

                CSqlString.RunSql(sSql,GetConnection());
                //sError = CSqlString.RunSql("delete from t_api_qd_process_step where process_id=" + listView1.SelectedItems[0].Text, GetConnection());
                //sError = CSqlString.RunSql("delete from t_api_qd_process_step_params where process_id=" + listView1.SelectedItems[0].Text, GetConnection());
                
                
                LoadProcess();
            }
        }
    }
}
